package info.qingshui.browser.hk.file;


import java.io.File;


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.util.Log;


public class FileHelper {


	/**
	 * 在SD卡上创建文件
	 * 
	 * @throws IOException 没有SD卡或SD卡坏了
	 */
	public static File creatFile(String fileName) throws IOException {
		File file = new File(fileName);
		file.createNewFile();
		return file;
	}


	
	/**
	 * 删除文件
	 * 
	 * @param fileName
	 */
	public static boolean delFile(String fileName) {
		File file = new File(fileName);
		if (file == null || !file.exists() || file.isDirectory())
			return false;
		file.delete();
		return true;
	}

	/**
	 * 创建目录
	 * 
	 * @param dirName
	 */
	public static File creatDir(String dirName) {
		File dir = new File(dirName);
		dir.mkdir();
		return dir;
	}

	/**
	 * 删除目录
	 * 
	 * @param dirName
	 */
	public static boolean delDir(String dirName) {
		File dir = new File(dirName);
		return delDir(dir);
	}

	/**
	 * 修改文件或目录名
	 * 
	 * @param fileName
	 */
	public static boolean renameSDFile(String oldfileName, String newFileName) {
		File oleFile = new File(oldfileName);
		File newFile = new File(newFileName);
		return oleFile.renameTo(newFile);
	}

	/**
	 * 拷贝单个文件
	 * 
	 * @param path
	 * @throws IOException
	 */
	public static boolean copyFileTo(String srcFileName, String destFileName)
			throws IOException {
		File srcFile = new File(srcFileName);
		File destFile = new File(destFileName);
		return copyFileTo(srcFile, destFile);
	}

	/**
	 * 拷贝指定目录的所有文件
	 * 
	 * @param srcDirName
	 * @param destDirName
	 * @return
	 * @throws IOException
	 */
	public static boolean copyFilesTo(String srcDirName, String destDirName)
			throws IOException {
		File srcDir = new File(srcDirName);
		File destDir = new File(destDirName);
		return copyFilesTo(srcDir, destDir);
	}

	/**
	 * 移动单个文件
	 * 
	 * @param srcFileName
	 * @param destFileName
	 * @return
	 * @throws IOException
	 */
	public static boolean moveFileTo(String srcFileName, String destFileName)
			throws IOException {
		File srcFile = new File(srcFileName);
		File destFile = new File(destFileName);
		return moveFileTo(srcFile, destFile);
	}

	/**
	 * 移动指定目录的所有文件
	 * 
	 * @param srcDirName
	 * @param destDirName
	 * @return
	 * @throws IOException
	 */
	public static boolean moveFilesTo(String srcDirName, String destDirName)
			throws IOException {
		File srcDir = new File(srcDirName);
		File destDir = new File(destDirName);
		return moveFilesTo(srcDir, destDir);
	}



	/**
	 * 删除一个文件
	 * 
	 * @param file
	 * @return
	 */
	public static boolean delFile(File file) {
		if (file.isDirectory())
			return false;
		return file.delete();
	}

	/**
	 * 删除一个目录（可以是非空目录）
	 * 
	 * @param dir
	 */
	public static boolean delDir(File dir) {
		if (dir == null || !dir.exists() || dir.isFile()) {
			return false;
		}
		for (File file : dir.listFiles()) {
			if (file.isFile()) {
				file.delete();
			} else if (file.isDirectory()) {
				delDir(file);// 递归
			}
		}
		dir.delete();
		return true;
	}

	/**
	 * 拷贝一个文件,srcFile源文件，destFile目标文件
	 * 
	 * @param path
	 * @throws IOException
	 */
	public static boolean copyFileTo(File srcFile, File destFile) throws IOException {
		if (srcFile.isDirectory() || destFile.isDirectory())
			return false;// 判断是否是文件
		FileInputStream fis = new FileInputStream(srcFile);
		FileOutputStream fos = new FileOutputStream(destFile);
		int readLen = 0;
		byte[] buf = new byte[1024];
		while ((readLen = fis.read(buf)) != -1) {
			fos.write(buf, 0, readLen);
		}
		fos.flush();
		fos.close();
		fis.close();
		return true;
	}

	/**
	 * 拷贝目录下的所有文件到指定目录
	 * 
	 * @param srcDir
	 * @param destDir
	 * @return
	 * @throws IOException
	 */
	public static boolean copyFilesTo(File srcDir, File destDir) throws IOException {
		Log.d(FileHelper.class.getSimpleName(), "copyFilesTo..." +srcDir.isDirectory() +
				destDir.isDirectory() + destDir.exists());
		Log.d(FileHelper.class.getSimpleName(), "src=" +srcDir.exists());
		if (!srcDir.isDirectory() || !destDir.isDirectory())
			return false;// 判断是否是目录
		if (!destDir.exists())
			return false;// 判断目标目录是否存在
		Log.d(FileHelper.class.getSimpleName(), "copyFilesTo start" );
		File[] srcFiles = srcDir.listFiles();
		for (int i = 0; i < srcFiles.length; i++) {
			if (srcFiles[i].isFile()) {
				// 获得目标文件
				File destFile = new File(destDir.getPath() + "//"
						+ srcFiles[i].getName());
				copyFileTo(srcFiles[i], destFile);
			} else if (srcFiles[i].isDirectory()) {
				File theDestDir = new File(destDir.getPath() + "//"
						+ srcFiles[i].getName());
				copyFilesTo(srcFiles[i], theDestDir);
			}
		}
		return true;
	}
	

	/**
	 * 移动一个文件
	 * 
	 * @param srcFile
	 * @param destFile
	 * @return
	 * @throws IOException
	 */
	public static boolean moveFileTo(File srcFile, File destFile) throws IOException {
		boolean iscopy = copyFileTo(srcFile, destFile);
		if (!iscopy)
			return false;
		delFile(srcFile);
		return true;
	}

	/**
	 * 移动目录下的所有文件到指定目录
	 * 
	 * @param srcDir
	 * @param destDir
	 * @return
	 * @throws IOException
	 */
	public static boolean moveFilesTo(File srcDir, File destDir) throws IOException {
		if (!srcDir.isDirectory() || !destDir.isDirectory()) {
			return false;
		}
		File[] srcDirFiles = srcDir.listFiles();
		for (int i = 0; i < srcDirFiles.length; i++) {
			if (srcDirFiles[i].isFile()) {
				File oneDestFile = new File(destDir.getPath() + "//"
						+ srcDirFiles[i].getName());
				moveFileTo(srcDirFiles[i], oneDestFile);
				delFile(srcDirFiles[i]);
			} else if (srcDirFiles[i].isDirectory()) {
				File oneDestFile = new File(destDir.getPath() + "//"
						+ srcDirFiles[i].getName());
				moveFilesTo(srcDirFiles[i], oneDestFile);
				delDir(srcDirFiles[i]);
			}

		}
		return true;
	}
	

}
